/**
 *====================================================
 * 文件名称: FinanceClaimCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年12月10日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.action.v1.dto;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.CollectionUtils;
import com.yuexunit.finance.project.action.v1.model.input.ClaimParams;
import com.yuexunit.finance.project.action.v1.model.output.ClaimAcceptanceDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimAcceptanceLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimAssessmentDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimAssessmentLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimCorrectiveDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimCorrectiveLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimHandleDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimHandleLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimMaterialDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimMaterialLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimReportCaseDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimReportCaseLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimSettlementDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimSettlementLineInfo;
import com.yuexunit.finance.project.action.v1.model.output.ClaimSurveyDetail;
import com.yuexunit.finance.project.action.v1.model.output.ClaimSurveyLineInfo;
import com.yuexunit.finance.project.entity.AbnormalEntity;
import com.yuexunit.finance.project.entity.ClaimAcceptanceEntity;
import com.yuexunit.finance.project.entity.ClaimAssessmentEntity;
import com.yuexunit.finance.project.entity.ClaimCorrectiveEntity;
import com.yuexunit.finance.project.entity.ClaimCoverageEntity;
import com.yuexunit.finance.project.entity.ClaimEntity;
import com.yuexunit.finance.project.entity.ClaimHandleEntity;
import com.yuexunit.finance.project.entity.ClaimMaterialEntity;
import com.yuexunit.finance.project.entity.ClaimReportCaseEntity;
import com.yuexunit.finance.project.entity.ClaimSettlementEntity;
import com.yuexunit.finance.project.entity.ClaimSurveyEntity;
import com.yuexunit.finance.project.entity.InsuranceCoverageEntity;
import com.yuexunit.finance.project.entity.InsuranceEntity;
import com.yuexunit.finance.project.service.AbnormalService;
import com.yuexunit.finance.project.service.ClaimAcceptanceService;
import com.yuexunit.finance.project.service.ClaimAssessmentService;
import com.yuexunit.finance.project.service.ClaimCorrectiveService;
import com.yuexunit.finance.project.service.ClaimHandleService;
import com.yuexunit.finance.project.service.ClaimMaterialService;
import com.yuexunit.finance.project.service.ClaimReportCaseService;
import com.yuexunit.finance.project.service.ClaimSettlementService;
import com.yuexunit.finance.project.service.ClaimSurveyService;
import com.yuexunit.finance.project.service.InsuranceCoverageService;
import com.yuexunit.finance.project.service.InsuranceService;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: FinanceClaimCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年12月10日 下午6:05:38
 */
public class FinanceClaimCastor {

	/** 创建理赔 */
	public static ClaimEntity fromClaim(ClaimParams params) {
		ClaimEntity claim = new ClaimEntity();
		claim.setClaimRemark(params.getClaimRemark());
		claim.setClaimDate(DateUtil.parseDateInt(params.getClaimDate()));
		claim.setClaimAmount(BigDecimal.ZERO);
		for (BigDecimal claimAmount : params.getClaimAmount()) {
			claim.setClaimAmount(claim.getClaimAmount().add(claimAmount));
		}
		AbnormalService abnormalService = ComponentFactory.loadClass(AbnormalService.class);
		AbnormalEntity abnormal = abnormalService.inquireAbnormal(params.getAbnormalId());
		claim.setAbnormalId(abnormal.getAbnormalId());
		claim.setAbnormalNumber(abnormal.getAbnormalNumber());
		claim.setAbnormalState(abnormal.getAbnormalState());

		InsuranceService insuranceService = ComponentFactory.loadClass(InsuranceService.class);
		InsuranceEntity insurance = insuranceService.inquireInsuranceEntity(params.getInsuranceId());
		claim.setInsuranceId(insurance.getInsuranceId());
		claim.setInsuranceNumber(insurance.getInsuranceNumber());
		claim.setInsuranceCompany(insurance.getInsuranceCompany());
		claim.setFinanceId(insurance.getFinanceId());
		claim.setFinanceNumber(insurance.getFinanceNumber());
		return claim;
	}

	/** 理赔险种信息 */
	public static List<ClaimCoverageEntity> fromClaim(ClaimEntity claim, ClaimParams params) {
		List<ClaimCoverageEntity> claimCoverages = new ArrayList<>();
		InsuranceCoverageService insuranceCoverageService = ComponentFactory.loadClass(InsuranceCoverageService.class);
		for (int i = 0; i < params.getInsuranceCoverageId().length; i++) {
			InsuranceCoverageEntity insuranceCoverage = insuranceCoverageService.inquireInsuranceCoverage(params.getInsuranceCoverageId()[i]);
			ClaimCoverageEntity coverage = new ClaimCoverageEntity();
			coverage.setClaimAmount(CollectionUtils.getArrayIndexValue(params.getClaimAmount(), i));
			coverage.setClaimId(claim.getClaimId());
			coverage.setIndemnityAmount(null);
			coverage.setIndemnityDate(null);
			coverage.setInsuranceCoverageId(insuranceCoverage.getInsuranceCoverageId());
			claimCoverages.add(coverage);
		}
		return claimCoverages;
	}

	/** 报案 */
	public static ClaimReportCaseDetail toClaimReportCaseDetail(ClaimReportCaseEntity entity) {
		ClaimReportCaseDetail info = new ClaimReportCaseDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 报案 */
	public static ClaimReportCaseLineInfo toClaimReportCaseLineInfo(ClaimReportCaseEntity entity) {
		ClaimReportCaseLineInfo info = new ClaimReportCaseLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 材料准备 */
	public static ClaimMaterialDetail toClaimMaterialDetail(ClaimMaterialEntity entity) {
		ClaimMaterialDetail info = new ClaimMaterialDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 材料准备 */
	public static ClaimMaterialLineInfo toClaimMaterialLineInfo(ClaimMaterialEntity entity) {
		ClaimMaterialLineInfo info = new ClaimMaterialLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 报案受理 */
	public static ClaimAcceptanceDetail toClaimAcceptanceDetail(ClaimAcceptanceEntity entity) {
		ClaimAcceptanceDetail info = new ClaimAcceptanceDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 报案受理 */
	public static ClaimAcceptanceLineInfo toClaimAcceptanceLineInfo(ClaimAcceptanceEntity entity) {
		ClaimAcceptanceLineInfo info = new ClaimAcceptanceLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 调查阶段 */
	public static ClaimSurveyDetail toClaimSurveyDetail(ClaimSurveyEntity entity) {
		ClaimSurveyDetail info = new ClaimSurveyDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 调查阶段 */
	public static ClaimSurveyLineInfo toClaimSurveyLineInfo(ClaimSurveyEntity entity) {
		ClaimSurveyLineInfo info = new ClaimSurveyLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 评估阶段 */
	public static ClaimAssessmentDetail toClaimAssessmentDetail(ClaimAssessmentEntity entity) {
		ClaimAssessmentDetail info = new ClaimAssessmentDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 评估阶段 */
	public static ClaimAssessmentLineInfo toClaimAssessmentLineInfo(ClaimAssessmentEntity entity) {
		ClaimAssessmentLineInfo info = new ClaimAssessmentLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 法院处置 */
	public static ClaimHandleDetail toClaimHandleDetail(ClaimHandleEntity entity) {
		ClaimHandleDetail info = new ClaimHandleDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 法院处置 */
	public static ClaimHandleLineInfo toClaimHandleLineInfo(ClaimHandleEntity entity) {
		ClaimHandleLineInfo info = new ClaimHandleLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 赔付处理 */
	public static ClaimSettlementDetail toClaimSettlementDetail(ClaimSettlementEntity entity) {
		ClaimSettlementDetail info = new ClaimSettlementDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 赔付处理 */
	public static ClaimSettlementLineInfo toClaimSettlementLineInfo(ClaimSettlementEntity entity) {
		ClaimSettlementLineInfo info = new ClaimSettlementLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 整改信息 */
	public static ClaimCorrectiveDetail toClaimCorrectiveDetail(ClaimCorrectiveEntity entity) {
		ClaimCorrectiveDetail info = new ClaimCorrectiveDetail();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	/** 整改信息 */
	public static ClaimCorrectiveLineInfo toClaimCorrectiveLineInfo(ClaimCorrectiveEntity entity) {
		ClaimCorrectiveLineInfo info = new ClaimCorrectiveLineInfo();
		BeanUtils.copyProperties(entity, info);
		info.setAuthenticatState(entity.getAuthenticatState().ordinal());
		return info;
	}

	public static void completeClaimResponse(ClaimEntity entity, ClaimLineInfo info) {
		BeanUtils.copyProperties(entity, info);
		info.setAbnormalState(entity.getAbnormalState().ordinal());
		info.setClaimDate(DateUtil.formatDate(entity.getClaimDate()));
		info.setClaimState(entity.getClaimState().ordinal());
	}

	/** 理赔明细 */
	public static ClaimDetail toClaimDetail(ClaimEntity entity) {
		ClaimDetail info = new ClaimDetail();
		completeClaimResponse(entity, info);
		ClaimReportCaseService claimReportCaseService = ComponentFactory.loadClass(ClaimReportCaseService.class);
		List<ClaimReportCaseEntity> reportCases = claimReportCaseService.inquireClaimReportCaseList(entity.getClaimId());
		for (ClaimReportCaseEntity reportCase : reportCases) {
			info.getClaimReportCases().add(toClaimReportCaseLineInfo(reportCase));
		}
		ClaimMaterialService claimMaterialService = ComponentFactory.loadClass(ClaimMaterialService.class);
		List<ClaimMaterialEntity> materials = claimMaterialService.inquireClaimMaterialList(entity.getClaimId());
		for (ClaimMaterialEntity material : materials) {
			info.getClaimMaterials().add(toClaimMaterialLineInfo(material));
		}
		ClaimAcceptanceService claimAcceptanceService = ComponentFactory.loadClass(ClaimAcceptanceService.class);
		List<ClaimAcceptanceEntity> acceptances = claimAcceptanceService.inquireClaimAcceptanceList(entity.getClaimId());
		for (ClaimAcceptanceEntity acceptance : acceptances) {
			info.getClaimAcceptances().add(toClaimAcceptanceLineInfo(acceptance));
		}
		ClaimSurveyService claimSurveyService = ComponentFactory.loadClass(ClaimSurveyService.class);
		List<ClaimSurveyEntity> surveies = claimSurveyService.inquireClaimSurveyList(entity.getClaimId());
		for (ClaimSurveyEntity survey : surveies) {
			info.getClaimSurveys().add(toClaimSurveyLineInfo(survey));
		}
		ClaimAssessmentService claimAssessmentService = ComponentFactory.loadClass(ClaimAssessmentService.class);
		List<ClaimAssessmentEntity> assessments = claimAssessmentService.inquireClaimAssessmentList(entity.getClaimId());
		for (ClaimAssessmentEntity assessment : assessments) {
			info.getClaimAssessments().add(toClaimAssessmentLineInfo(assessment));
		}
		ClaimHandleService claimHandleService = ComponentFactory.loadClass(ClaimHandleService.class);
		List<ClaimHandleEntity> handles = claimHandleService.inquireClaimHandleList(entity.getClaimId());
		for (ClaimHandleEntity handle : handles) {
			info.getClaimHandles().add(toClaimHandleLineInfo(handle));
		}
		ClaimSettlementService claimSettlementService = ComponentFactory.loadClass(ClaimSettlementService.class);
		List<ClaimSettlementEntity> settlements = claimSettlementService.inquireClaimSettlementList(entity.getClaimId());
		for (ClaimSettlementEntity settlement : settlements) {
			info.getClaimSettlements().add(toClaimSettlementLineInfo(settlement));
		}
		ClaimCorrectiveService claimCorrectiveService = ComponentFactory.loadClass(ClaimCorrectiveService.class);
		List<ClaimCorrectiveEntity> correctives = claimCorrectiveService.inquireClaimCorrectiveList(entity.getClaimId());
		for (ClaimCorrectiveEntity corrective : correctives) {
			info.getClaimCorrectives().add(toClaimCorrectiveLineInfo(corrective));
		}
		return info;
	}

	/** 理赔列表 */
	public static ClaimLineInfo toClaimLineInfo(ClaimEntity entity) {
		ClaimLineInfo info = new ClaimLineInfo();
		completeClaimResponse(entity, info);
		return info;
	}

}
